package cn.dsp.admin.controller;

import cn.dsp.admin.entity.RolePerm;
import cn.dsp.admin.entity.admin.Role;
import cn.dsp.admin.service.IRoleService;
import cn.dsp.admin.service.ex.ServiceException;
import cn.dsp.admin.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;


    @RequestMapping("add_new")
    public JsonResult<Void> addRole(Role role,HttpSession session){
        String adminName = getAdminNameFromSession(session);
        try {
            roleService.addRole(role,adminName);
        }catch (ServiceException e){
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS);
    }

    /**
     * http://localhost:8088/role/change_info?roleId=2&pids=1&pids=2&pids=3&pids=4
     * */
    @RequestMapping("change_info")
    public JsonResult<Void> changeInfo(Role role, Integer[] pids, HttpSession session){
        String modifiedUser = getAdminNameFromSession(session);
        try {
            roleService.changeInfo(role.getRoleId(),modifiedUser,role);
            Integer roleId = role.getRoleId();
            //清空角色原有的权限
            roleService.deleteAllPermByRid(roleId);
            for (Integer p: pids) {
                //创建一个rolePerm封装角色的权限
                RolePerm rolePerm = new RolePerm();
                rolePerm.setRoleId(roleId);
                rolePerm.setPid(p);
                //插入新的权限
                roleService.roleAddPerm(rolePerm);
            }
        } catch (ServiceException e) {
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS);
    }


    @RequestMapping("get_role_all")
    public JsonResult<List<Role>> getAllRole(){
        List<Role> data = roleService.getAllRole();
        return new JsonResult<>(SUCCESS,data);
    }


    @RequestMapping("get_role_like/{roleName}")
    public JsonResult<List<Role>> getLikeRoleName(@PathVariable("roleName") String roleName){
        System.out.println("roleName:"+roleName);
        List<Role> data = roleService.getLikeRoleName(roleName);
        return new JsonResult<>(SUCCESS,data);
    }

    @RequestMapping("get_by_roleName")
    public JsonResult<Role> getByRoleName(String roleName){
        Role data = roleService.getByRoleName(roleName);
        return new JsonResult<>(SUCCESS,data);
    }

    @RequestMapping("get_role_by/{roleId}")
    public JsonResult<Role> getByRoleId(@PathVariable("roleId") Integer roleId){
        System.out.println("RoleController.getByRoleId().roleId:"+roleId);
        Role data = roleService.getByRoleId(roleId);
        return new JsonResult<>(SUCCESS,data);
    }

    @RequestMapping("delete_role_by/{roleId}")
    public JsonResult<Void> deleteByRoleId(@PathVariable("roleId") Integer roleId){
        roleService.deleteByRoleId(roleId);
        return new JsonResult<>(SUCCESS);
    }



}
